<?php
class Hoithao_Model_Hoithao extends Zend_Db_Table{
	
	protected $_name = 'hoithao';
	protected $_primary ='id';
	
	protected $_ids;
	
	public function getItem($arrParam = null, $options = null){
		
		if($options['task'] == 'admin-info' || $options['task'] == 'admin-edit'){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter, $config);
			$select = $db->select()
			->from('hoithao AS c')
// 			->joinLeft('hoithao AS cc', 'c.id_coso = cc.id',array('cc.name AS name_coso'))
//  			->joinLeft('users AS u', 'c.id_user = u.id',array('u.user_name AS name_chunhiem'))
			->where('c.id = ?', $arrParam['id'], INTEGER);
			
			$result = $db->fetchRow($select);
		}
		
		if($options['task'] == 'public-detail'){
			$select = $this->select()
					->where('id = ?',$arrParam['id'],INTERGER);
			$result = $this->fetchRow($select)->toArray();
		}
		
		if($options['task'] == 'public-category'){
			if(!empty($arrParam['cid'])){
				$db = Zend_Registry::get('connectDb');
				//$db = Zend_Db::factory($adapter, $config);
				$select = $db->select()
				->from('quan_club_coso AS cc')
				->where('cc.id = ?', $arrParam['cid'], INTEGER);
				$result = $db->fetchRow($select);
			}else{
				$language = Zend_Registry::get('language');
				$result = array('name' => $language['language']['baiViet'],
						'meta_title' => '',
						'meta_description' => '',
						'meta_keywords' => ''
				);
			}
		}
		
		return $result;
	}
	
	public function countItem($arrParam = null, $options = null){
		$ssFilter  = $arrParam['ssFilter'];
		if($options == null){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter, $config);
			$select = $db->select()
			->from('hoithao AS c', array('COUNT(c.id) AS totalItem'));
			
			$result = $db->fetchOne($select);
		}
		if($options['task'] == 'admin-list'){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter, $config);
			
			$select = $db->select()
			->from('hoithao AS c', array('COUNT(c.id) AS totalItem'));
			
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('c.name LIKE ?', $keywords, STRING);
			}
			
			if(!empty($ssFilter['blocks'])){
				$select->where('c.' . $ssFilter['blocks'] . '= ?', 1, INTERGER);
			}
			
			$result = $db->fetchOne($select);
		}
		
		if($options['task'] == 'public-category'){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter, $config);
			if(!empty($arrParam['cid'])){
				$select = $db->select()
				->from('quan_club_clb AS c',array('COUNT(c.id) AS totalItem'))
				->where('c.status = ?',1,INTERGER);
				if(!empty($this->_ids)){
					$select->where('c.cat_id IN (' . $this->_ids . ')');
				}
			}else{
				$select = $db->select()
				->from('quan_club_clb AS c',array('COUNT(c.id) AS totalItem'))
				->where('c.status = ?',1,INTERGER);
			}
			
			if(!empty($ssFilter['lang_code'])){
				$select->where("c.lang_code = '" . $ssFilter['lang_code'] . "'");
			}
			
			$result = $db->fetchOne($select);
		}
		
		if($options['task'] == 'public-search'){
			$ssFilter  = $arrParam['ssFilter'];
			$db = Zend_Registry::get('connectDb');
			$select = $db->select()
			->from('quan_club_clb AS c',array('COUNT(c.id) AS totalItem'))
			->where('c.status = ?',1,INTERGER);
		
			if(!empty($ssFilter['keywords'])){
				$select->where("(c.name like '%" . $ssFilter['keywords'] . "%') OR (c.alias like '%" . $ssFilter['keywords'] . "%')");
			}
		
			if(!empty($ssFilter['lang_code'])){
				$select->where("c.lang_code = '" . $ssFilter['lang_code'] . "'");
			}
			$result = $db->fetchOne($select);
		}
		
		if($options['task'] == 'public-tags'){
			$ssFilter  = $arrParam['ssFilter'];
			$db = Zend_Registry::get('connectDb');
			$select = $db->select()
			->from('quan_club_clb AS c',array('COUNT(c.id) AS totalItem'))
			->where('c.status = ?',1,INTERGER);
		
			if(!empty($arrParam['key'])){
				$select->where("c.tags like '%" . $arrParam['key'] . "%'");
			}
		
			if(!empty($ssFilter['lang_code'])){
				$select->where("c.lang_code = '" . $ssFilter['lang_code'] . "'");
			}
			$result = $db->fetchOne($select);
		}
		return $result;
	}
	
	public function itemInSelectbox($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		$ssFilter  = $arrParam['ssFilter'];
		if($options == null){
			$select = $db->select()
			->from('quan_club_coso AS cc',array('id','name','status'))
			->order('cc.id ASC');
			$result = $db->fetchPairs($select);
			
		}
		//var_dump($result);die;
		return $result;
	}
	
	public function userInSelectbox($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		$ssFilter  = $arrParam['ssFilter'];
		if($options == null){
			$select = $db->select()
			->from('users AS cc',array('id','name','status'))
			->order('cc.id ASC');
			$result = $db->fetchPairs($select);
				
		}
		//var_dump($result);die;
		return $result;
	}
	
	public function listItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter, $config);
		$ssFilter  = $arrParam['ssFilter'];
		if($options['task'] == 'admin-list'){
			
			$paginator = $arrParam['paginator'];
			
			
			$select = $db	->	select()
							->from('hoithao AS c',array("*","DATE_FORMAT(c.date_start,'%d/%m/%Y') AS date_start","DATE_FORMAT(c.date_end,'%d/%m/%Y') AS date_end"));
// 							->joinLeft('quan_club_coso AS cc', 'c.id_coso = cc.id',array('cc.name AS name_coso'))
// 							->joinLeft('users AS u', 'c.id_user = u.id',array('u.name AS name_chunhiem'));
							
							//->where('c.id = ?', $arrParam['id'], INTEGER);
				
			
			if($paginator['itemCountPerPage'] > 0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page, $rowCount);
			}
			$result = $db->fetchAll($select);
		}
		
		if($options['task'] == 'public-category'){
			if(!empty($arrParam['cid'])){
				$select = $db->select()
						->from('customer_category',array('id','name','parents'))
						->where('status = ?',1,INTERGER);
				$resultCategory = $db->fetchAll($select);
				$system = new Zendvn_System_Recursive($resultCategory);
				$newArray = $system->builArray($arrParam['cid']);
				$tmp[] = $arrParam['cid'];
				if(count($newArray)>0){
					foreach ($newArray as $key => $val){
						$tmp[] = $val['id'];
					}
				}
				$ids = implode(',', $tmp);
				$this->_ids = $ids;
				$paginator = $arrParam['paginator'];
				$select = $db->select()
				->from('customer AS c',array('id','name','alias','picture','thumb','synopsis','cat_id','created','hits'))
				->join('customer_category AS cc', 'cc.id = c.cat_id',array('name AS category_name', 'cc.alias AS category_alias'))
				->where('c.status = ?',1,INTERGER)
				->where('c.cat_id IN (' . $ids . ')')
				->order('id DESC');
			}else{
				$paginator = $arrParam['paginator'];
				$select = $db->select()
				->from('customer AS c',array('id','name','alias','picture','thumb','synopsis','cat_id','created','hits'))
				->join('customer_category AS cc', 'cc.id = c.cat_id',array('name AS category_name', 'cc.alias AS category_alias'))
				->where('c.status = ?',1,INTERGER)
				->order('id DESC');
			}
			
			if($paginator['itemCountPerPage'] > 0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page, $rowCount);
			}
				
			if(!empty($ssFilter['lang_code'])){
				$select->where("c.lang_code = '" . $ssFilter['lang_code'] . "'");
			}
			
			$result = $db->fetchAll($select);
		}
		
		if($options['task'] == 'public-old'){
			$select = $db->select()
			->from('customer_category',array('id','name','parents'))
			->where('status = ?',1,INTERGER);
			$resultCategory = $db->fetchAll($select);
			$system = new Zendvn_System_Recursive($resultCategory);
			$newArray = $system->builArray($arrParam['cid']);
			$tmp[] = $arrParam['cid'];
			if(count($newArray)>0){
				foreach ($newArray as $key => $val){
					$tmp[] = $val['id'];
				}
			}
			$ids = implode(',', $tmp);
			$this->_ids = $ids;

			$select = $db->select()
			->from('customer AS c',array('id','name','alias','picture','thumb','synopsis','cat_id','created','hits'))
			->join('customer_category AS cc', 'cc.id = c.cat_id',array('name AS category_name', 'cc.alias AS category_alias'))
			->where('c.status = ?',1,INTERGER)
			->where('c.cat_id IN (' . $ids . ')')
			->where('c.id < ?', $arrParam['id'], INTERGER)
			->order('id DESC')
			->limit(10,0);
			if(!empty($ssFilter['lang_code'])){
				$select->where("c.lang_code = '" . $ssFilter['lang_code'] . "'");
			}
			$result = $db->fetchAll($select);
		}
		
		if($options['task'] == 'public-new'){
			$select = $db->select()
			->from('customer_category',array('id','name','parents'))
			->where('status = ?',1,INTERGER);
			$resultCategory = $db->fetchAll($select);
			$system = new Zendvn_System_Recursive($resultCategory);
			$newArray = $system->builArray($arrParam['cid']);
			$tmp[] = $arrParam['cid'];
			if(count($newArray)>0){
				foreach ($newArray as $key => $val){
					$tmp[] = $val['id'];
				}
			}
			$ids = implode(',', $tmp);
			$this->_ids = $ids;
		
			$select = $db->select()
			->from('customer AS c',array('id','name','alias','picture','thumb','synopsis','cat_id','created','hits'))
			->join('customer_category AS cc', 'cc.id = c.cat_id',array('name AS category_name', 'cc.alias AS category_alias'))
			->where('c.status = ?',1,INTERGER)
			->where('c.cat_id IN (' . $ids . ')')
			->where('c.id > ?', $arrParam['id'], INTERGER)
			->order('id DESC')
			->limit(10,0);
			if(!empty($ssFilter['lang_code'])){
				$select->where("c.lang_code = '" . $ssFilter['lang_code'] . "'");
			}
			$result = $db->fetchAll($select);
		}
		
		if($options['task'] == 'public-search'){
			$ssFilter  = $arrParam['ssFilter'];
			$paginator = $arrParam['paginator'];
			$select = $db->select()
			->from('customer AS c',array('id','name','alias','picture','thumb','synopsis','cat_id','created','hits'))
			->join('customer_category AS cc', 'cc.id = c.cat_id',array('name AS category_name', 'cc.alias AS category_alias'))
			->where('c.status = ?',1,INTERGER)
			->order('c.id DESC');
				
			if(!empty($ssFilter['keywords'])){
				$select->where("(c.name like '%" . $ssFilter['keywords'] . "%') OR (c.alias like '%" . $ssFilter['keywords'] . "%')");
			}
		
			if($paginator['itemCountPerPage'] > 0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page, $rowCount);
			}
		
			if(!empty($ssFilter['lang_code'])){
				$select->where("c.lang_code = '" . $ssFilter['lang_code'] . "'");
			}
		
			$result = $db->fetchAll($select);
		}
		
		if($options['task'] == 'public-tags'){
			$ssFilter  = $arrParam['ssFilter'];
			$paginator = $arrParam['paginator'];
			$select = $db->select()
			->from('customer AS c',array('id','name','alias','picture','thumb','synopsis','cat_id','created','hits'))
			->join('customer_category AS cc', 'cc.id = c.cat_id',array('name AS category_name', 'cc.alias AS category_alias'))
			->where('c.status = ?',1,INTERGER)
			->order('c.id DESC');
		
			if(!empty($arrParam['key'])){
				$select->where("c.tags like '%" . $arrParam['key'] . "%'");
			}
		
			if($paginator['itemCountPerPage'] > 0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page, $rowCount);
			}
		
			$result = $db->fetchAll($select);
		}
		
		return $result;
		
	}

	public function saveItem($arrParam = null, $options = null){
		//var_dump($arrParam);die;
		if($options['task'] == 'admin-add'){
			$row 				= $this->fetchNew();
// 			if(empty($arrParam['alias'])){
// 				$filter = new Zendvn_Filter_RewriteUrl();
// 				$alias 	= $filter->filter($arrParam['name']);
// 			}else{
// 				$alias 	= $arrParam['alias'];
// 			}
			$info 					= new Zendvn_System_Info();
			$created_by 			= $info->getMemberInfo('id');
			
// 			$thumb = new Zendvn_Filter_GetThumb();

			$row->name 			= stripslashes($arrParam['name']);
			$row->content 		= stripslashes($arrParam['content']);
			
			$date_start     = new Zend_Date();
			$date_start->set($arrParam['date_start']);
			$date_start    = $date_start->toString('YYYY-MM-dd');
			$row->date_start 			= $date_start;
				
			$date_end     = new Zend_Date();
			$date_end->set($arrParam['date_end']);
			$date_end    = $date_end->toString('YYYY-MM-dd');
			$row->date_end 			= $date_end;
			
			$row->soluongnguoi 	= stripslashes($arrParam['soluongnguoi']);
			$row->ketqua 			= stripslashes($arrParam['ketqua']);
			$row->muctieu 			= stripslashes($arrParam['muctieu']);
			$row->diadiem 			= stripslashes($arrParam['diadiem']);
			$row->created 			= @date("Y-m-d h:i:s");
			$row->created_by		= $created_by;
			$row->status 			= $arrParam['status'];
			$row->gia 		= $arrParam['gia'];

			$row->save();
		}
	
		if($options['task'] == 'admin-edit'){
				
			$where 				= ' id=' . $arrParam['id'];
			$row 				= $this->fetchRow($where);
		
			$row->name 			= stripslashes($arrParam['name']);
			$row->content 		= stripslashes($arrParam['content']);
			
			$date_start     = new Zend_Date();
			$date_start->set($arrParam['date_start']);
			$date_start    = $date_start->toString('YYYY-MM-dd');
			$row->date_start 			= $date_start;
			
			$date_end     = new Zend_Date();
			$date_end->set($arrParam['date_end']);
			$date_end    = $date_end->toString('YYYY-MM-dd');
			$row->date_end 			= $date_end;
			
			
			$row->soluongnguoi 	= stripslashes($arrParam['soluongnguoi']);
			$row->ketqua 			= stripslashes($arrParam['ketqua']);
			$row->muctieu 			= stripslashes($arrParam['muctieu']);
			$row->diadiem 			= stripslashes($arrParam['diadiem']);
			$row->created 			= @date("Y-m-d h:i:s");
			$row->created_by		= $created_by;
			$row->status 			= $arrParam['status'];
// 			$row->meta_title 		= stripslashes($arrParam['meta_title']);
// 			$row->meta_description 	= stripslashes($arrParam['meta_description']);
// 			$row->meta_keywords 	= stripslashes($arrParam['meta_keywords']);
			$row->gia 		= $arrParam['gia'];
			
			$row->save();
		}
		
		if($options['task'] == 'public-hits'){
			$where 				= ' id=' . $arrParam['id'];
			$row 				= $this->fetchRow($where);
			
			$row->hits 		= $arrParam['hits'] + 1;
			
			$row->save();
		}
	}

	public function deleteItem($arrParam = null, $options = null){
		if($options['task'] == 'admin-delete'){				
			$where = ' id=' . $arrParam['id'];
			$result = $this->delete($where);
		}
	
		if($options['task'] == 'admin-delete-muti'){
			$cid = explode(',', $arrParam['cid']);
			if(!empty($cid) && isset($arrParam['cid'])){
				$ids = implode(',', $cid);
				$where = 'id IN (' . $ids . ')';
				$this->delete($where);
			}
	
		}
	}
	
	public function changeStatus($arrParam = null, $options = null){
		$cid = $arrParam['cid'];
		if(count($cid) > 0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
	
			$id = implode(',', $cid);
			$data = array('status' => $status);
			$where = 'id IN (' . $id . ')';
			$this->update($data, $where);
		}
		if($arrParam['id'] > 0){
			if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
			$data = array('status' => $status);
			$where = 'id = ' . $arrParam['id'];
			$this->update($data, $where);
		}
	}
	
	public function sortItem($arrParam = null, $options = null){
		$cid = $arrParam['cid'];
		$order = $arrParam['order'];
		if($options['task'] == 'admin-sort'){
			if(count($cid) > 0){
				foreach ($cid as $key => $val){
					$data = array('order' => $order[$val]);
					$where = 'id = ' . $val;
					$this->update($data, $where);
				}
			}
		}
	}
}